package com.voicechat.firstmet.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.GifListAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.GifBean;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.PersonDataService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity
 * @ClassName: GifListActivity
 * @Description: 他人礼物列表
 * @Author: ljp
 * @CreateDate: 2018/11/17 10:28
 * @Version: 1.0
 */
public class GifListActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_gif)
    RecyclerView recyclerGif;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private String objectId;
    private String anchorId;
    private List<GifBean.PageBean.ListBean> gifBeans;
    private GifListAdapter gifAdapter;
    public MySelfInfoBean.ObjectBean mySeleInfo;
    private String userId;
    private String token;
    int page = 1;
    private String deviceInfo;
    private String TAG = "GifListActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_gif_list;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        objectId = getIntent().getStringExtra("objectId");
        anchorId = getIntent().getStringExtra("anchorId");
        initView();
    }

    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2018/11/17 11:35
     * @author: ljp
     */
    private void initView() {
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        userId = mySeleInfo.getId();
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        if(!TextUtils.isEmpty(anchorId) && anchorId.equals(mySeleInfo.getId())){
            tvTitle.setText("我的礼物");
        }
        gifBeans = new ArrayList<>();
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initHttpGif();
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHttpGif();
                smartRefresh.finishLoadMore(10000);
            }
        });
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        gifAdapter = new GifListAdapter(context, gifBeans, R.layout.item_gif_list);
        recyclerGif.setLayoutManager(new LinearLayoutManager(activity));
        recyclerGif.setAdapter(gifAdapter);
        initHttpGif();
    }

    /**
     * @return
     * @method
     * @description 请求礼物列表
     * @date: 2018/11/17 12:26
     * @author: ljp
     */
    private void initHttpGif() {
        if (!TextUtils.isEmpty(objectId)) {
            PersonDataService.showPersonGif(token, deviceInfo, objectId, PersonDataService.GIF_TYPE.GIF_PLAY, page + "").subscribe(gifBean -> {
                if (gifBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                    int i = gifBean.getPage().getCount() / gifBean.getPage().getPageSize() +
                            (gifBean.getPage().getCount() % gifBean.getPage().getPageSize() == 0 ? 0 : 1);
                    if (page > i) {//判断是否是最后一页{//判断是否是最后一页
                        smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                    } else {
                        if (page == 1) {
                            gifBeans.clear();
                            smartRefresh.finishRefresh();
                        }
                        gifBeans.addAll(gifBean.getPage().getList());
                        gifAdapter.notifyDataSetChanged();
                        smartRefresh.finishLoadMore();
                    }
                }else if(gifBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                    Utils.setToken(context);
                }else {
                    toast_MakeText(gifBean.getMsg());
                }
            }, throwable -> {

            });
        } else if (!TextUtils.isEmpty(anchorId)) {
            PersonDataService.personHomeGifAll(token, deviceInfo, anchorId, page + "").subscribe(gifBean -> {
                if (gifBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                    int i = gifBean.getPage().getCount() / gifBean.getPage().getPageSize() +
                            (gifBean.getPage().getCount() % gifBean.getPage().getPageSize() == 0 ? 0 : 1);
                    if (page > i) {//判断是否是最后一页{//判断是否是最后一页
                        smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                    } else {
                        if (page == 1) {
                            gifBeans.clear();
                            smartRefresh.finishRefresh();
                        }
                        gifBeans.addAll(gifBean.getPage().getList());
                        gifAdapter.notifyDataSetChanged();
                        smartRefresh.finishLoadMore();
                    }
                }else if(gifBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                    Utils.setToken(context);
                }else {
                    toast_MakeText(gifBean.getMsg());
                }
            }, throwable -> {
                toast_MakeText(getResources().getString(R.string.net_error));

            });
        }

    }


    @OnClick(R.id.linear_back)
    public void onClick() {
        finish();
    }
}
